package org.zpmis.common.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;
import org.zpmis.common.result.ResultCode;
import org.zpmis.common.utils.language.LanguageUtils;


/**
 * Description:
 *  业务异常类
 *  EqualsAndHashCode 启用父类属性参与运算
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 2332608236621015980L;

    protected String code;

    protected String msg;

    protected ResultCode resultCode;

    protected Object data;

    public BusinessException() {

    }

    public BusinessException(Integer code,String msg) {
        this();
        this.code = code.toString();
        this.msg = msg;
    }

    public BusinessException(String format, Object... objects) {
        this();
        format = StringUtils.replace(format, "{}", "%s");
        this.msg = String.format(format, objects);
    }


    public BusinessException(ResultCode resultCode, Object data) {
        this(resultCode);
        this.data = data;
    }

    public BusinessException(ResultCode resultCode, Object[] args) {
        this(resultCode);
        String message = LanguageUtils.getMessage(resultCode.name(), args);
        this.msg = message;
    }

    public BusinessException(ResultCode resultCode) {
        super(resultCode.message());
        this.resultCode = resultCode;
        this.code = resultCode.code().toString();
        this.msg = resultCode.message();
    }


}
